package com.example.demo.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 返回值输出过滤器，这里用来加密返回值
 * @author LF
 * @date 2021/4/16 - 16:12
 */
public class MyFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        System.out.println("-----doFilter-----");
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String uri = request.getRequestURI();
        //  1、排除
        Boolean aBoolean = StringUtils.isNotBlank(uri);
        if (!aBoolean) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        // 转换成代理类
        ResponseWrapper wrapperResponse = new ResponseWrapper(response);
        // 这里只拦截返回，直接让请求过去，如果在请求前有处理，可以在这里处理
        filterChain.doFilter(request, wrapperResponse);
        // 返回值字节
        byte[] content = wrapperResponse.getContent();
        // 判断是否有值
        if (content.length <= 0) {
            return;
        }
        // 返回值
        String str = new String(content, "UTF-8");
        // 调用链
        if (!isJson(str)) {
            // 如果不是json数据,可能是返回的数据流或下载文件,直接返回原数据
            //把返回值输出到客户端
            ServletOutputStream out = response.getOutputStream();
            out.write(content);
            out.flush();
        } else {
            // 处理返回数据
            String cipherText = null;
            try {
                cipherText = str;
                JSONObject jsonObject = JSON.parseObject(str);
                System.out.println(str + "----------------------------------------->>>Filter");
                /*String records = jsonObject.getString("records");
                System.out.println(records);
                List<String> list = JSON.parseArray(records, String.class);
                list.forEach(l -> {
                    System.out.println(l);
                    if(!l.isEmpty()){
                        JSONObject object = JSON.parseObject(l);
                        String mobile1 = object.getString("mobile");
                        System.out.println(mobile1);
                    }
                });*/
                //......根据需要处理返回值
            } catch (Exception e) {
                e.printStackTrace();
            }
            //把返回值输出到客户端
            ServletOutputStream out = response.getOutputStream();
            out.write(cipherText.getBytes());
            out.flush();
        }
    }

    /**
     * 判断字符串数据是否为json
     * @param content
     * @return
     */
    public static boolean isJson(String content) {
        try {
            JSON.parseObject(content);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
